package com.carkeeper.user.controller;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.pojo.Role;
import com.carkeeper.role.service.RoleService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description 角色处理器
 * @Author yangbin
 * @Date 2020/11/23 14:43
 * @Version 1.0
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Role> roleList = roleService.findAll();
        try {
            return new Result(true, StatusCode.OK, "查询成功", roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 分页查询角色
     * @return
     */
    @GetMapping("/queryPageByExample/{currentPage}/{pageSize}")
    public Result<Role> queryPageByExample(@RequestBody Map searchMap,
                                           @PathVariable("currentPage") Integer currentPage,
                                           @PathVariable("pageSize") Integer pageSize) {
        Page page = roleService.findPage(searchMap, currentPage, pageSize);
        if (page.getTotal() <= 0) {
            return new Result<>(false, StatusCode.ERROR, "查询失败");
        }
        return new Result<>(true, StatusCode.OK, "查询成功", page);
    }

    /**
     * 根据id查询角色
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Role role = roleService.findById(id);
        try {
            return new Result(true, StatusCode.OK, "查询成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    /**
     * 新增角色
     * @return
     */
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        int i = roleService.addRole(role, permissionIds,menuIds);
        try {
            return new Result(true, StatusCode.OK, "新增成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增失败");
        }
    }

    /**
     * 修改角色
     * @return
     */
    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        int i = roleService.updateRole(role, permissionIds, menuIds);
        try {
            return new Result(true, StatusCode.OK, "修改成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    /**
     * 根据id删除角色
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        int i = roleService.deleteById(id);
        try {
            return new Result(true, StatusCode.OK, "删除成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
    }
}
